﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Telmex;

namespace Confree
{
    /// <summary>
    /// Window for configuring Confree.
    /// </summary>
    public partial class ConfigureWindow : Form
    {
        /// <summary>
        /// Instantiates a new configuration window.
        /// </summary>
        public ConfigureWindow()
        {
            InitializeComponent();
        }

        private void ConfigureWindow_Load(object sender, EventArgs e)
        {
            SetUIFromConfiguration();
        }

        private void SetUIFromConfiguration()
        {
            var section = ConfigurationManager.GetSection("telmex") as TelmexConfigurationSection;

            textboxUsername.Text = section.ApiUsername;
            textboxPassword.Text = section.ApiSecret;
            textboxResponsibleId.Text = section.DefaultResponsibleId;
            textboxResponsibleEmail.Text = section.DefaultResponsibleEmail;
        }

        private void SetConfigurationFromUI()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var section = config.GetSection("telmex") as TelmexConfigurationSection;

            section.ApiUsername = textboxUsername.Text;
            section.ApiSecret = textboxPassword.Text;
            section.DefaultResponsibleId = textboxResponsibleId.Text;
            section.DefaultResponsibleEmail = textboxResponsibleEmail.Text;

            config.Save();
            ConfigurationManager.RefreshSection("telmex");
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            SetConfigurationFromUI();
            this.Close();
        }

        private void linkCancel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }
    }
}
